import { sendAddGoods, getAllGoods, sendDeleteGoods, sendQueryGoods, sendUpdateGoods } from '@/services/goods';

export const fetchingAddGoodsListLoading = ({ loading }) => loading.effects['goods/getAllGoodsList'];

export default {
  namespace: 'goods',

  state: {
    // 列表及分页
    list: [],
    pagination: {},
  },

  effects: {
    *getAllGoodsList({ payload }, { call, put }) {
      const result = yield call(getAllGoods);
      yield put({
        type: 'saveGoodsList',
        payload: result,
      });
    },
    *addGoods({ payload }, { call, put }) {
      const result = yield call(sendAddGoods, payload);
      // console.log(result);
      // 重新拉取列表，重新渲染
      yield put({
        type: 'getAllGoodsList',
      });
    },
    *deleteGoods({ payload }, { call, put }) {
      // console.log(payload);
      const result = yield call(sendDeleteGoods, payload);
      // 重新拉取列表，重新渲染
      yield put({
        type: 'getAllGoodsList',
      });
    },
    *queryGoods({ payload }, { call, put }) {
      // console.log(payload);
      const result = yield call(sendQueryGoods, payload);
      // console.log(result);
      yield put({
        type: 'showQueryResult',
        payload: result,
      });
    },
    *updateGoods({ payload }, { call, put }) {
      const result = yield call(sendUpdateGoods, payload);
      // 重新拉取列表，重新渲染
      yield put({
        type: 'getAllGoodsList',
      });
    },
  },

  reducers: {
    saveGoodsList(state, { payload }) {
      // console.log(payload);
      return {
        ...state,
        list: payload,
      };
    },
    showQueryResult(state, { payload }) {
      // console.log(payload);
      return {
        ...state,
        list: payload,
      };
    },
  },
};
// export default GoodsModel;
